package main

import "strconv"

// 整数反转
func reverse(x int) int {
	s := strconv.Itoa(x)
	runes := []rune(s)
	sign := "" // 负号
	if runes[0] == '-' {
		sign = string(runes[0]) // 暂存负号
		runes = 
		runes[1:] // 舍弃负号不参与反转运算
	}
	for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
		runes[from], runes[to] = runes[to], runes[from]
	}
	if runes[0] == '0' {
		runes = runes[1:] // 首位数为0则舍弃
	}
	s = string(runes)
	if sign != "" {
		s = sign + s // 补上负号
	}
	i32, err := strconv.Atoi(s)
	if err != nil || i32 < -2147483648 || i32 > 2147483647 {
		i32 = 0
	}
	return i32
}
